var t = true;

Page({
    calcFinished: false,
    data: {
        keys: [ {
            key: [ {
                k: "1"
            }, {
                k: "5"
            }, {
                k: "..."
            } ]
        }, {
            key: [ {
                k: "2"
            }, {
                k: "6"
            }, {
                k: "9"
            } ]
        }, {
            key: [ {
                k: "3"
            }, {
                k: "7"
            }, {
                k: "0"
            } ]
        }, {
            key: [ {
                k: "4"
            }, {
                k: "8"
            }, {
                k: "."
            } ]
        }, {
            key: [ {
                k: "←"
            }, {
                k: "确定",
                cc: "ok"
            } ]
        } ],
        scrollTop: 0,
        popHeight: 80,
        popView: true,
        fingerFlag: false,
        qtyArrary: [],
        qtyArraryMode: [],
        showArrary: [],
        resultArrary: [],
        rightWrongArrary: [],
        keyIndex: 0,
        ttKeyIndex: {
            index: 0,
            result: ""
        },
        keyBoardResult: ""
    },
    selectFrom: function(t, e) {
        var r = e - t + 1;
        return Math.floor(Math.random() * r + t);
    },
    keySwitch: function(e) {
        var r = e.currentTarget.dataset.showid, a = this.data.ttKeyIndex, s = this.data.keyIndex, i = r, n = this.data.showArrary, o = this.data.resultArrary, h = this.data.qtyArrary, d = this.data.rightWrongArrary;
        if (this.data.popView || (this.setData({
            popHeight: 370,
            popView: true,
            fingerFlag: true
        }), t = false), !n[r]) {
            n.splice(r, 1, true), o.splice(r, 1, h[r].result), d.splice(r, 1, "right"), a = {
                index: r,
                result: h[r].result
            };
            for (var l = false, u = i + 1; u < h.length; u++) if ("" == o[u] || o[u] != h[u].result) {
                l = true, s = u;
                break;
            }
            if (!l) for (u = 0; u <= i; u++) if ("" == o[u] || o[u] != h[u].result) {
                l = true, s = u;
                break;
            }
            this.setData({
                showArrary: n,
                resultArrary: o,
                rightWrongArrary: d,
                ttKeyIndex: a,
                keyIndex: s
            }), l || (wx.showModal({
                showCancel: false,
                title: "您已经做完了",
                content: "请返回主页重新选择.....",
                success: function(t) {
                    t.confirm && wx.navigateBack({
                        delta: 2
                    });
                }
            }), this.setData({
                popHeight: 80,
                popView: false,
                fingerFlag: false
            }), t = true);
        }
    },
    onLoad: function(t) {
        for (var e = [], r = [], a = [], s = 0; s < t.qty; s++) e[s] = false, r[s] = "", a[s] = "blank";
        this.setData({
            showArrary: e,
            resultArrary: r,
            rightWrongArrary: a,
            opt: t
        }), this.fnCode(t.id, t.qty), wx.setNavigationBarTitle({
            title: t.subTitle
        });
    },
    randomNum: function(t) {
        for (var e = [], r = 2; r <= t; r++) e.push(r);
        return e.sort(function() {
            return .5 - Math.random();
        }), e.length = t - 1, e;
    },
    fnOne: function(t, e, r) {
        for (var a = [], s = 0, i = 0, n = 0; n < e; n++) {
            for (;;) {
                var o = (c = this.randomNum(t))[0], h = c[1];
                if (o % 10 != 0 && h % 10 != 0 && o % 10 != parseInt(o / 10) && h % 10 != parseInt(h / 10) && (o + h) % 10 != 0 && Math.abs(o - h) % 10 != 0) {
                    s = o, i = h;
                    break;
                }
            }
            if (s + i <= t) 1 == r ? a.push({
                content: s + "+" + i + "=",
                result: s + i
            }) : a.push({
                content: (.1 * s).toFixed(1) + "+" + (.1 * i).toFixed(1) + "=",
                result: (.1 * s + .1 * i).toFixed(1)
            }); else {
                var d = Math.max(s, i), l = Math.min(s, i);
                d - l == 1 && (l -= this.selectFrom(2, 3)), 1 == r ? a.push({
                    content: d + "-" + l + "=",
                    result: d - l
                }) : a.push({
                    content: (.1 * d).toFixed(1) + "-" + (.1 * l).toFixed(1) + "=",
                    result: (.1 * d - .1 * l).toFixed(1)
                });
            }
            for (var u = 0; u < n; u++) if (a[n].content == this.data.qtyArrary[u].content) {
                for (;;) {
                    var c = this.randomNum(t);
                    o = c[0], h = c[1];
                    if (o % 10 != 0 && h % 10 != 0 && o % 10 != parseInt(o / 10) && h % 10 != parseInt(h / 10) && Math.abs(o - h) % 10 != 0 && 1 != Math.abs(o - h)) {
                        s = Math.max(o, h), i = Math.min(o, h);
                        break;
                    }
                }
                if (1 == r) y = {
                    content: s + "-" + i + "=",
                    result: s - i
                }; else var y = {
                    content: (.1 * s).toFixed(1) + "-" + (.1 * i).toFixed(1) + "=",
                    result: (.1 * s - .1 * i).toFixed(1)
                };
                a.splice(n, 1, y);
            }
            this.setData({
                qtyArrary: a
            });
        }
    },
    fnTwo: function(t, e, r, a, s) {
        for (var i = [], n = 0, o = 0, h = 0, d = 0; d < e; d++) {
            for (;;) {
                var l = (k = this.randomNum(t))[0], u = k[1], c = k[2];
                if (l % 10 != 0 && u % 10 != 0 && c % 10 != 0 && l % 10 != parseInt(l / 10) && u % 10 != parseInt(u / 10) && c % 10 != parseInt(c / 10) && (l + u) % 10 != 0) {
                    n = l, o = u, h = c;
                    break;
                }
            }
            if (n + o < a) n + o + h <= t ? 1 == r ? i.push({
                content: n + "+" + o + "+" + h + "=",
                result: n + o + h
            }) : i.push({
                content: (.1 * n).toFixed(1) + "+" + (.1 * o).toFixed(1) + "+" + (.1 * h).toFixed(1) + "=",
                result: (.1 * (n + o + h)).toFixed(1)
            }) : (p = n + o + h - (y = Math.max(n, o, h)) - (f = Math.min(n, o, h)), 1 == r ? i.push({
                content: y + "-" + p + "+" + f + "=",
                result: y - p + f
            }) : i.push({
                content: (.1 * y).toFixed(1) + "-" + (.1 * p).toFixed(1) + "+" + (.1 * f).toFixed(1) + "=",
                result: (.1 * (y - p + f)).toFixed(1)
            })); else if (n + o >= a && n + o < t) n + o > h ? 1 == r ? i.push({
                content: n + "+" + o + "-" + h + "=",
                result: n + o - h
            }) : i.push({
                content: (.1 * n).toFixed(1) + "+" + (.1 * o).toFixed(1) + "-" + (.1 * h).toFixed(1) + "=",
                result: (.1 * (n + o - h)).toFixed(1)
            }) : 1 == r ? i.push({
                content: h + "-(" + n + "+" + o + ")=",
                result: h - n - o
            }) : i.push({
                content: (.1 * h).toFixed(1) + "-(" + (.1 * n).toFixed(1) + "+" + (.1 * o).toFixed(1) + ")=",
                result: (.1 * (h - n - o)).toFixed(1)
            }); else {
                var y = Math.max(n, o, h), f = Math.min(n, o, h), p = n + o + h - y - f;
                y >= s ? 1 == r ? i.push({
                    content: y + "-(" + p + "-" + f + ")=",
                    result: y - (p - f)
                }) : i.push({
                    content: (.1 * y).toFixed(1) + "-(" + (.1 * p).toFixed(1) + "-" + (.1 * f).toFixed(1) + ")=",
                    result: (.1 * (y - (p - f))).toFixed(1)
                }) : 1 == r ? i.push({
                    content: y + "-" + p + "+" + f + "=",
                    result: y - p + f
                }) : i.push({
                    content: (.1 * y).toFixed(1) + "-" + (.1 * p).toFixed(1) + "+" + (.1 * f).toFixed(1) + "=",
                    result: (.1 * (y - p + f)).toFixed(1)
                });
            }
            for (var x = 0; x < d; x++) if (i[d].content == this.data.qtyArrary[x].content) {
                for (;;) {
                    var k = this.randomNum(t);
                    l = k[0], u = k[1], c = k[2];
                    if (l % 10 != 0 && u % 10 != 0 && c % 10 != 0 && l % 10 != parseInt(l / 10) && u % 10 != parseInt(u / 10) && c % 10 != parseInt(c / 10) && (l + u) % 10 != 0) {
                        h = l + u + c - (n = Math.max(l, u, c)) - (o = Math.min(l, u, c));
                        break;
                    }
                }
                if (1 == r) F = {
                    content: n + "-" + h + "+" + o + "=",
                    result: n - h + o
                }; else var F = {
                    content: (.1 * n).toFixed(1) + "-" + (.1 * h).toFixed(1) + "+" + (.1 * o).toFixed(1) + "=",
                    result: (.1 * (n - h + o)).toFixed(1)
                };
                i.splice(d, 1, F);
            }
            this.setData({
                qtyArrary: i
            });
        }
    },
    fnThree: function(t, e, r, a, s, i) {
        for (var n = [], o = 0, h = 0, d = 0; d < s; d++) {
            for (;;) {
                var l = this.selectFrom(t, e), u = this.selectFrom(r, a);
                if (l * u % 10 != 0 && l % 10 != 0 && u % 10 != 0 && l % 10 != parseInt(l / 10) && u % 10 != parseInt(u / 10)) {
                    o = l, h = u;
                    break;
                }
            }
            1 == i ? n.push({
                content: o + "X" + h + "=",
                result: o * h
            }) : n.push({
                content: (.1 * o).toFixed(1) + "X" + (.1 * h).toFixed(1) + "=",
                result: (o * h * .1 * .1).toFixed(2)
            });
            for (var c = 0; c < d; c++) if (n[d].content == this.data.qtyArrary[c].content) {
                for (;;) {
                    var y = this.selectFrom(t, e), f = this.selectFrom(r, a);
                    if (y * f % 10 != 0 && y % 10 != 0 && f % 10 != 0 && y % 10 != parseInt(y / 10) && f % 10 != parseInt(f / 10)) {
                        o = y, h = f;
                        break;
                    }
                }
                if (1 == i) p = {
                    content: o + "X" + h + "=",
                    result: o * h
                }; else var p = {
                    content: (.1 * o).toFixed(1) + "X" + (.1 * h).toFixed(1) + "=",
                    result: (o * h * .1 * .1).toFixed(2)
                };
                n.splice(d, 1, p);
            }
            this.setData({
                qtyArrary: n
            });
        }
    },
    fnFour: function(t, e, r, a, s, i) {
        for (var n = [], o = 0, h = 0, d = 0; d < s; d++) {
            for (;;) {
                var l = this.selectFrom(t, e), u = this.selectFrom(r, a);
                if (l % 10 != 0 && u % 10 != 0 && l % u == 0) {
                    o = l, h = u;
                    break;
                }
            }
            1 == i ? n.push({
                content: o + "÷" + h + "=",
                result: o / h
            }) : h < 16 ? n.push({
                content: (.1 * o).toFixed(1) + "÷" + h + "=",
                result: (o / h * .1).toFixed(1)
            }) : o % 2 != 0 && o / h < 30 && h >= 16 ? n.push({
                content: (.1 * o).toFixed(1) + "÷" + (.1 * h).toFixed(1) + "=",
                result: o / h
            }) : n.push({
                content: (.01 * o).toFixed(2) + "÷" + (.1 * h).toFixed(1) + "=",
                result: (o / h * .1).toFixed(1)
            });
            for (var c = 0; c < d; c++) if (n[d].content == this.data.qtyArrary[c].content) {
                for (;;) {
                    var y = this.selectFrom(t, e), f = this.selectFrom(r, a);
                    if (y % 10 != 0 && f % 10 != 0 && y % f == 0) {
                        o = y, h = f;
                        break;
                    }
                }
                if (1 == i) p = {
                    content: o + "÷" + h + "=",
                    result: o / h
                }; else if (h < 16) p = {
                    content: (.1 * o).toFixed(1) + "÷" + h + "=",
                    result: (o / h * .1).toFixed(1)
                }; else if (o % 2 != 0 && o / h < 30 && h >= 16) p = {
                    content: (.1 * o).toFixed(1) + "÷" + (.1 * h).toFixed(1) + "=",
                    result: o / h
                }; else var p = {
                    content: (.01 * o).toFixed(2) + "÷" + (.1 * h).toFixed(1) + "=",
                    result: (o / h * .1).toFixed(1)
                };
                n.splice(d, 1, p);
            }
            this.setData({
                qtyArrary: n
            });
        }
    },
    fnFive: function(t) {
        for (var e = [], r = 0, a = 0, s = 0, i = 0, n = 0; n < t; n++) {
            for (;;) {
                var o = this.selectFrom(101, 999), h = this.selectFrom(11, 99);
                if (o % 10 != 0 && h % 10 != 0 && o % 10 != parseInt(o / 10) && h % 10 != parseInt(h / 10) && o % h != 0) {
                    r = o, a = h, s = parseInt(o / h), i = o % h;
                    break;
                }
            }
            e.push({
                content: r + "÷" + a + "=",
                result: s + "..." + i
            });
            for (var d = 0; d < n; d++) if (e[n].content == this.data.qtyArrary[d].content) {
                for (;;) {
                    o = this.selectFrom(101, 999), h = this.selectFrom(11, 99);
                    if (o % 10 != 0 && h % 10 != 0 && o % 10 != parseInt(o / 10) && h % 10 != parseInt(h / 10) && o % h != 0) {
                        r = o, a = h, s = parseInt(o / h), i = o % h;
                        break;
                    }
                }
                var l = {
                    content: r + "÷" + a + "=",
                    result: s + "..." + i
                };
                e.splice(n, 1, l);
            }
            this.setData({
                qtyArrary: e
            });
        }
    },
    fnSix: function(t) {
        for (var e = [], r = 0, a = 0, s = 0, i = 0; i < t; i++) {
            for (;;) {
                var n = this.selectFrom(11, 99), o = this.selectFrom(11, 99), h = this.selectFrom(101, 999);
                if (!(n * o > 1e3 || n * o % 10 == 0 || n % 10 == 0 || o % 10 == 0 || h % 100 == 0 || n % 10 == parseInt(n / 10) || o % 10 == parseInt(o / 10))) {
                    r = n, a = o, s = h;
                    break;
                }
            }
            n * o > 800 ? (y = r * a, e.push({
                content: s + "+" + y + "÷" + a + "=",
                result: s + r
            })) : h > n * o ? e.push({
                content: s + "-" + r + "X" + a + "=",
                result: s - r * a
            }) : e.push({
                content: r + "X" + a + "-" + s + "=",
                result: r * a - s
            });
            for (var d = 0; d < i; d++) if (e[i].content == this.data.qtyArrary[d].content) {
                for (;;) {
                    var l = this.selectFrom(11, 99), u = this.selectFrom(11, 99), c = this.selectFrom(101, 999);
                    if (!(l * u > 1e3 || l * u % 10 == 0 || l % 10 == 0 || u % 10 == 0 || c % 100 == 0 || l % 10 == parseInt(l / 10) || u % 10 == parseInt(u / 10))) {
                        r = l, a = u, s = c;
                        break;
                    }
                }
                var y = {
                    content: r + "X" + a + "+" + s + "=",
                    result: r * a + s
                };
                e.splice(i, 1, y);
            }
            this.setData({
                qtyArrary: e
            });
        }
    },
    fnCode: function(t, e) {
        switch (t) {
          case "20100":
            this.fnOne(20, e, 1);
            break;

          case "20200":
            this.fnTwo(20, e, 1, 16, 18);
            break;

          case "50100":
            this.fnOne(50, e, 1);
            break;

          case "50200":
            this.fnTwo(50, e, 1, 30, 40);
            break;

          case "10010":
            this.fnOne(100, e, 1);
            break;

          case "10020":
            this.fnTwo(100, e, 1, 50, 90);
            break;

          case "10001":
            this.fnOne(1e3, e, 1);
            break;

          case "10002":
            this.fnTwo(1e3, e, 1, 500, 900);
            break;

          case "xyz01":
            this.fnOne(99, e, .1);
            break;

          case "xyz02":
            this.fnTwo(99, e, .1, 50, 90);
            break;

          case "2a100":
            this.fnThree(11, 99, 2, 9, e, 1);
            break;

          case "2b100":
            this.fnFour(11, 99, 2, 9, e, 1);
            break;

          case "3a100":
            this.fnThree(101, 999, 2, 9, e, 1);
            break;

          case "3b100":
            this.fnFour(101, 999, 2, 9, e, 1);
            break;

          case "2a200":
            this.fnThree(11, 99, 11, 99, e, 1);
            break;

          case "3b200":
            this.fnFour(101, 999, 11, 99, e, 1);
            break;

          case "3a200":
            this.fnThree(101, 999, 11, 99, e, 1);
            break;

          case "xyz11":
            this.fnThree(11, 99, 11, 99, e, .1);
            break;

          case "xyz22":
            this.fnFour(101, 999, 3, 99, e, .1);
            break;

          case "xyz33":
            this.fnFive(e);
            break;

          case "xyzab":
            this.fnSix(e);
        }
    },
    onShow: function() {
        this.setData({
            popHeight: 80,
            popView: false,
            fingerFlag: false
        }), t = true;
    },
    popKeyboard: function() {
        t ? (this.setRightWrongArrary(this.data.ttKeyIndex.index), this.setRightWrongArrary(this.data.keyIndex), 
        this.setData({
            popHeight: 370,
            popView: true,
            fingerFlag: true
        }), t = false) : (this.setRightWrongArrary(this.data.ttKeyIndex.index), this.setRightWrongArrary(this.data.keyIndex), 
        this.setData({
            popHeight: 80,
            popView: false,
            fingerFlag: false
        }), t = true);
    },
    setRightWrongArrary: function(t) {
        var e = this.data.rightWrongArrary, r = this.data.resultArrary, a = this.data.qtyArrary;
        this.data.keyIndex, this.data.ttKeyIndex, "" == r[t] ? e.splice(t, 1, "blank") : r[t] == a[t].result ? e.splice(t, 1, "right") : e.splice(t, 1, "wrong"), 
        this.setData({
            rightWrongArrary: e
        });
    },
    bindKey: function(t) {
        this.handleKey(t.target.id);
    },
    handleKey: function(e) {
        var r = this.data.showArrary, a = "";
        if (this.calcFinished ? (a = e, this.calcFinished = false) : a = this.data.keyBoardResult + e, 
        "←" == e) {
            if (!(i = (a = this.data.keyBoardResult).length)) return;
            a = "..." == (s = a.substring(i - 3, i)) ? a.substring(0, i - 3) : a.substring(0, i - 1);
        }
        if ("..." == e || "." == e) {
            if (!(i = (a = this.data.keyBoardResult).length)) return;
            if (a = this.data.keyBoardResult + e, i) {
                var s = a.substring(i - 1, i);
                if ("." == s) return;
            }
        }
        if ("确定" != e && "." != e && "←" != e) {
            var i = (a = this.data.keyBoardResult).length;
            if (1 == i && "0" == a) return;
            a = this.data.keyBoardResult + e;
        }
        if ("确定" == e) {
            this.calcFinished = true;
            var n = this.data.resultArrary;
            n.splice(this.data.keyIndex, 1, this.data.keyBoardResult);
            var o = "blank";
            this.data.keyBoardResult == this.data.qtyArrary[this.data.keyIndex].result || this.data.keyBoardResult + "." == this.data.qtyArrary[this.data.keyIndex].result || this.data.keyBoardResult + ".0" == this.data.qtyArrary[this.data.keyIndex].result ? (n.splice(this.data.keyIndex, 1, this.data.qtyArrary[this.data.keyIndex].result), 
            o = "right", r.splice(this.data.keyIndex, 1, true)) : o = "wrong";
            var h = this.data.rightWrongArrary;
            "" == this.data.keyBoardResult ? h.splice(this.data.keyIndex, 1, "blank") : h.splice(this.data.keyIndex, 1, o);
            for (var d = this.data.keyIndex, l = this.data.keyIndex, u = {
                index: d,
                result: this.data.resultArrary[d]
            }, c = false, y = l + 1; y < this.data.qtyArrary.length; y++) if ("" == this.data.resultArrary[y] || this.data.resultArrary[y] != this.data.qtyArrary[y].result) {
                c = true, d = y;
                break;
            }
            if (!c) for (y = 0; y <= l; y++) if ("" == this.data.resultArrary[y] || this.data.resultArrary[y] != this.data.qtyArrary[y].result) {
                c = true, d = y;
                break;
            }
            c || (wx.showModal({
                showCancel: false,
                title: "真棒！您做完了！",
                content: "小朋友一定要勤加练习哟！",
                success: function(t) {
                    t.confirm && wx.navigateBack({
                        delta: 2
                    });
                }
            }), this.setData({
                popHeight: 80,
                popView: false,
                fingerFlag: false
            }), t = true), this.setData({
                ttKeyIndex: u,
                keyIndex: d,
                scrollTop: 30 * d - 30,
                showArrary: r,
                resultArrary: n,
                rightWrongArrary: h
            }), a = "";
        }
        this.setData({
            keyBoardResult: a
        });
    },
    getKeyIndex: function(e) {
        var r = e.currentTarget.dataset.showid, a = this.data.rightWrongArrary;
        if (this.data.popView || (this.setData({
            popHeight: 370,
            popView: true,
            fingerFlag: true
        }), t = false), this.calcFinished = true, this.setData({
            keyBoardResult: ""
        }), this.setRightWrongArrary(this.data.ttKeyIndex.index), "right" != this.data.rightWrongArrary[r]) {
            if ("blank" == this.data.rightWrongArrary[r] || "wrong" == this.data.rightWrongArrary[r]) {
                var s = {
                    index: r,
                    result: this.data.resultArrary[r]
                };
                a.splice(r, 1, "blank"), this.setData({
                    ttKeyIndex: s,
                    rightWrongArrary: a
                });
            }
            this.setData({
                keyIndex: r
            });
        } else this.data.ttKeyIndex.result != this.data.qtyArrary[this.data.ttKeyIndex.index].result && (a.splice(this.data.ttKeyIndex.index, 1, "blank"), 
        this.setData({
            rightWrongArrary: a
        }));
    },
    onShareAppMessage: function() {
        return {
            title: this.data.opt.subTitle + "-算术题生成器",
            path: "/packageA/suanshu/index"
        };
    },
    onShareTimeline: function(t) {
        return {
            title: this.data.opt.subTitle + "-算术题生成器",
            path: "/packageA/suanshu/index"
        };
    }
});